#include "zf_common_headfile.h"

uint8 key_able = 1;
uint8 wireless_able = 0;
uint8 seekfree_able = 0;

//全外设与软件初始化
void user_init(){
    tft180_init();
    tft180_show_string(0, 0, "408 Lab. cWUI-V2");
    tft180_show_string(0, 16, "<USER_INIT>");
    tft180_show_string(0, 48, "DISPLAY");
    tft180_show_string(112, 48, "OK");

    tft180_show_string(0, 64, "KEY");
    tft180_show_string(0, 80, "CAMERA");
    tft180_show_string(0, 96, "IMU963RA");
    tft180_show_string(0, 112, "WIFI");

    // Key
    key_init(10);
    pit_ms_init(TIM6_PIT, 20);
    tft180_show_string(112, 64, "OK");

    // Camera
    while (1) {
        if (mt9v03x_init()) {
            tft180_show_string(112, 80, "RE");
        } else { // 成功！
            tft180_show_string(112, 80, "OK");
            break;
        }
        system_delay_ms(1000);
    }

    // Encoder
    Encoder_init();
    // Motor
    Suspension_motor_init();                         //悬浮电机
    Left_motor_init();                               //左翼动力电机
    Right_motor_init();                              //右翼动力电机
    Motor_duty_clear();
    pit_ms_init(TIM9_PIT, 20);                       //电机输出
    pit_ms_init(TIM7_PIT, 50);                       //控制方向

    // PID
    pid_param_init();

    // MPU
    CalibrateToZero();
    pit_ms_init(TIM8_PIT, 50);
    tft180_show_string(112, 96, "OK");
    
    // FLASH
    tft180_clear();
    tft180_show_string(column(0),line(0),"FLASH?");
    tft180_show_string(column(0),line(2),"K1 - Read");
    tft180_show_string(column(0),line(5),"Ot - No");

    switch (ogas_get_key(1)){
        case K1_L:
        case K1_S:
            flash_read();
            break;
        default:
            break;
    }

    // WIRELESS
    tft180_clear();
    tft180_show_string(column(0),line(0),"WIRELESS?");
    tft180_show_string(column(0),line(2),"K1 - Y");
    tft180_show_string(column(0),line(5),"Ot - N");

    switch (ogas_get_key(1)){
        case K1_L:
        case K1_S:
            wireless_uart_init();
            wireless_able = 1;
            wireless_uart_send_string("Welcome to use OGAS!");
            break;
        default:break;
    }
    if (wireless_able) {
        tft180_clear();
        tft180_show_string(column(0),line(0),"SEEKFREE?");
        tft180_show_string(column(0),line(2),"K1 - Y");
        tft180_show_string(column(0),line(5),"Ot - N");

        switch (ogas_get_key(1)){
            case K1_L:
            case K1_S:
                seekfree_able = 1;
                seekfree_assistant_interface_init (SEEKFREE_ASSISTANT_WIRELESS_UART);
                seekfree_assistant_camera_information_config(SEEKFREE_ASSISTANT_MT9V03X, bin_image[0], 80, 60);
                //seekfree_assistant_camera_boundary_config (X_BOUNDARY, image_h, center_line, l_border, r_border, NULL, NULL, NULL);
                seekfree_assistant_camera_boundary_config (XY_BOUNDARY, (uint16)USE_num, NULL, points_l_x, points_r_x, NULL, points_l_y, points_r_y);
                break;
            default:break;
        }
    }
}
